home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / tcplusx.zip / EVENTMGR.CPP < prev    next >
C/C++ Source or Header  |  1991-02-28  |  11KB  |  391 lines

  1. //
  2. // eventmgr.cpp - implementation of class EventManager
  3. // Author        - Robin W. McKean
  4. // Last Update    - February 23,1991
  5. // Copyright (C) 1991 All rights reserved
  6. //
  7. // This file remains the property of the author, Robin W. McKean.  You are
  8. // free to use and change it as you see fit.  This module, nor its object
  9. // code, may not however be included  in any packaged software without the
  10. // written consent of the author.
  11. //
  12.  
  13. // Contents ----------------------------------------------------------------
  14. //
  15. //        EventManager::EventManager
  16. //        EventManager::~EventManager
  17. //        EventManager::isA
  18. //        EventManager::nameOf
  19. //        EventManager::hashValue
  20. //        EventManager::isEqual
  21. //        EventManager::printOn
  22. //        EventManager::putEvent
  23. //        EventManager::getEvent
  24. //        EventManager::positionDevice
  25. //        EventManager::hideDevice
  26. //        EventManager::showDevice
  27. //        EventManager::addDevice
  28. //        EventManager::subtractDevice
  29. //
  30. // Description
  31. //
  32. //        The purpose if this class is to command and control all of the
  33. //        devices that a programmer wants to use as input.  The devices will
  34. //        feed events to the event manager, and the programmer can access
  35. //        those events by calling EventManager::getEvent( Event& ).  Also, the
  36. //        programmer can put their own events into the queue by calling
  37. //        EventManager::putEvent( Event&, int ).
  38. //
  39. // End ---------------------------------------------------------------------
  40.  
  41. // Interface dependencies --------------------------------------------------
  42.  
  43. #ifndef _EVENTMGR_H
  44. #include <eventmgr.h>
  45. #endif
  46.  
  47. #ifndef _IOSTREAM_H
  48. #include <iostream.h>
  49. #endif
  50.  
  51. #ifndef _STDLIB_H
  52. #include <stdlib.h>
  53. #define _STDLIB_H
  54. #endif
  55.  
  56. // End Interface dependencies ----------------------------------------------
  57.  
  58. // Implementation dependencies ---------------------------------------------
  59.  
  60. #ifndef _DEVICE_H
  61. #include <device.h>
  62. #endif
  63.  
  64. // End Implementation dependencies -----------------------------------------
  65.  
  66. // Member Function //
  67.  
  68. EventManager::EventManager( int maxEvents ) : queueList(), deviceList(), freeList()
  69.  
  70. // Description -------------------------------------------------------------
  71. //
  72. //        The constructor allocates our events, and sets up our lists for
  73. //        for input processing
  74. //
  75. // End ---------------------------------------------------------------------
  76. {
  77.     eventMax = maxEvents;
  78.  
  79.     if( eventMax <= 0 || eventMax >= 100 )
  80.         eventMax = 100;                     // Maximum    events
  81.  
  82.     events = new Event[ eventMax ];
  83.     if( !events ) return;                    // from the heap
  84.  
  85. // Body Comment ------------------------------------------------------------
  86. //
  87. //        We add the events from the array to the free list.    This saves
  88. //        memory and allows us to insure that events are there to be processed
  89. //
  90. // End ---------------------------------------------------------------------
  91.  
  92.     for( int i = 0; i < eventMax; i++ ) freeList.add( events[ i ] );
  93. }
  94.  
  95. // End EventManager::EventManager //
  96.  
  97. // Member Function //
  98.  
  99. EventManager::~EventManager( void )
  100.  
  101. // Description -------------------------------------------------------------
  102. //
  103. //        The destructor detaches all of the events from both lists, and then
  104. //        destroys the events it allocated
  105. //
  106. // End ---------------------------------------------------------------------
  107. {
  108.     while( !queueList.isEmpty() )
  109.     {
  110.         Object& theObject = queueList.getLeft();
  111.     }
  112.  
  113.     while( !freeList.isEmpty() )
  114.     {
  115.         Object& theOtherObject = freeList.peekAtHead();
  116.         freeList.detachFromHead( theOtherObject );
  117.     }
  118.  
  119.     delete [ eventMax ]events;
  120. }
  121.  
  122. // End EventManager::~EventManager //
  123.  
  124. // Member Function //
  125.  
  126. classType EventManager::isA( ) const
  127.  
  128. // Description -------------------------------------------------------------
  129. //
  130. //        Returns the numeric equivelent of the class type
  131. //
  132. // End ---------------------------------------------------------------------
  133. {
  134.     return eventManagerClass;
  135. }
  136.  
  137. // End EventManager::isA //
  138.  
  139. // Member Function //
  140.  
  141. char *EventManager::nameOf( ) const
  142.  
  143. // Description -------------------------------------------------------------
  144. //
  145. //        Returns the character string equivelent of a class
  146. //
  147. // End ---------------------------------------------------------------------
  148. {
  149.     return "EventManager";
  150. }
  151.  
  152. // End EventManager::nameOf //
  153.  
  154. // Member Function //
  155.  
  156. hashValueType EventManager::hashValue( ) const
  157.  
  158. // Description -------------------------------------------------------------
  159. //
  160. //        There is no hash value for an event manager, so return 0.
  161. //
  162. // End ---------------------------------------------------------------------
  163. {
  164.     return hashValueType( 0 );
  165. }
  166.  
  167. // End EventManager::hashValue //
  168.  
  169. // Member Function //
  170.  
  171. #pragma argsused
  172. int EventManager::isEqual( const Object& theEventManager ) const
  173.  
  174. // Description -------------------------------------------------------------
  175. //
  176. //        Determines if one event manager is equivelent to another
  177. //
  178. // End ---------------------------------------------------------------------
  179. {
  180.     return( 0 );
  181. }
  182.  
  183. // End EventManager::isEqual //
  184.  
  185. // Member Function //
  186.  
  187. void EventManager::printOn( ostream& theOutputStream ) const
  188.  
  189. // Description -------------------------------------------------------------
  190. //
  191. //        The event manager does not really have any of its own information
  192. //        to output to the screen.  So what we will do is dump the device
  193. //        list, queue list, and the free list.  I hope you have your pause
  194. //        button ready to be pressed.
  195. //
  196. // End ---------------------------------------------------------------------
  197. {
  198.     deviceList.printOn( theOutputStream );
  199.     queueList.printOn( theOutputStream );
  200.     freeList.printOn( theOutputStream );
  201. }
  202.  
  203. // End EventManager::printOn //
  204.  
  205. void EventManager::putEvent( Event& theEvent, int atBeginning )
  206.  
  207. // Description -------------------------------------------------------------
  208. //
  209. //        We attempt to put this event into the queue.  We first check to see
  210. //        if a free event is available.  If it is, we add it at the head or
  211. //        the tail, depending on the status of atBeginning.
  212. //
  213. // End ---------------------------------------------------------------------
  214. {
  215.     if( freeList.isEmpty() ) return;
  216.  
  217.     Event& theNewEvent = ( Event& )freeList.peekAtHead();
  218.     freeList.detachFromHead( theNewEvent );
  219.  
  220.     theNewEvent = theEvent;
  221.  
  222.     if( atBeginning ) queueList.putLeft( theNewEvent );
  223.     else queueList.putRight( theNewEvent );
  224.  
  225. }
  226.  
  227. // EventManager::putEvent //
  228.  
  229. // Member Function //
  230.  
  231. void EventManager::getEvent( Event& theEvent )
  232.  
  233. // Description -------------------------------------------------------------
  234. //
  235. //        We check to see if there is something in the queue.  If there is not
  236. //        then we continually poll the devices until an event is put into our
  237. //        queue.
  238. //
  239. // End ---------------------------------------------------------------------
  240. {
  241.     ContainerIterator& device = deviceList.initIterator();
  242.  
  243.     while( queueList.isEmpty() )
  244.     {
  245.         while( ( int )device ) ( ( Device& )( device++ ) ).pollDevice();
  246.         device.restart();
  247.     }
  248.  
  249.     Event& theNewEvent = ( Event& )queueList.getLeft( );
  250.     freeList.add( theNewEvent );
  251.     theEvent = theNewEvent;
  252.  
  253.     // Must destroy the device because it is allocated from the heap
  254.     delete &device;
  255. }
  256.  
  257. // End EventManager::Get //
  258.  
  259. // Member Function //
  260.  
  261. void EventManager::positionDevice( classType deviceType, int row, int column )
  262.  
  263. // Description -------------------------------------------------------------
  264. //
  265. //        This function will position the requested device at the coordinates
  266. //        specified in row and column.
  267. //
  268. // End ---------------------------------------------------------------------
  269. {
  270.     ContainerIterator& device = deviceList.initIterator();
  271.  
  272.     while( ( int )device )
  273.     {
  274.         Device& theDevice = ( Device& )device++;
  275.  
  276.         if( theDevice.isA() == deviceType )
  277.         {
  278.             Event theEvent;
  279.             theEvent.type = S_DEVICE;
  280.             theEvent.typeCode = D_POSITION;
  281.             theEvent.point.row = row;
  282.             theEvent.point.column = column;
  283.             theDevice.processA( theEvent );
  284.         }
  285.                      
  286.     }
  287.     delete &device;
  288. }
  289.  
  290. // End EventManager::positionDevice //
  291.  
  292. // Member Function //
  293.  
  294. void EventManager::hideDevice( classType deviceType )
  295.  
  296. // Description -------------------------------------------------------------
  297. //
  298. //        The requested device will be hidden if it is in the list
  299. //
  300. // End ---------------------------------------------------------------------
  301. {
  302.     ContainerIterator& device = deviceList.initIterator();
  303.  
  304.     while( ( int )device )
  305.     {
  306.         Device& theDevice = ( Device& )device++;
  307.  
  308.         if( deviceType == theDevice.isA() )
  309.         {
  310.             Event theEvent;
  311.             theEvent.type = S_DEVICE;
  312.             theEvent.typeCode = D_HIDE;
  313.             theDevice.processA( theEvent );
  314.         }
  315.  
  316.     }
  317.     delete &device;
  318. }                                                
  319.  
  320. // End EventManager::hideDevice //
  321.  
  322.  
  323. // Member Function //
  324.  
  325. void EventManager::showDevice( classType deviceType )
  326.  
  327. // Description -------------------------------------------------------------
  328. //
  329. //        The requested device will be revealed if it is in the list
  330. //
  331. // End ---------------------------------------------------------------------
  332. {
  333.     ContainerIterator& device = deviceList.initIterator();
  334.  
  335.     while( ( int )device )
  336.     {
  337.         Device& theDevice = ( Device& )device++;
  338.  
  339.         if( deviceType == theDevice.isA() )
  340.         {
  341.             Event theEvent;
  342.             theEvent.type = S_DEVICE;
  343.             theEvent.typeCode = D_SHOW;
  344.             theDevice.processA( theEvent );
  345.         }
  346.     }
  347.  
  348.     delete &device;
  349. }
  350.  
  351. // End EventManager::showDevice //
  352.  
  353. // Member Function //
  354.  
  355. void EventManager::addDevice( Object& theDevice )
  356.  
  357. // Description -------------------------------------------------------------
  358. //
  359. //        Simple method for adding your device to the list for polling
  360. //
  361. // End ---------------------------------------------------------------------
  362. {
  363.     deviceList.add( theDevice );
  364.     ( ( Device& )theDevice ).theEventManager = this;
  365.  
  366.     Event theEvent;
  367.  
  368.     // Now we need to initialize our device
  369.     theEvent.type = S_DEVICE;
  370.     theEvent.typeCode = D_INIT;
  371.     ( ( Device& )theDevice ).processA( theEvent );
  372. }
  373.  
  374. // End EventManager::addDevice //
  375.  
  376. // Member Function //
  377.  
  378. void EventManager::subtractDevice( Object& theDevice )
  379.  
  380. // Description -------------------------------------------------------------
  381. //
  382. //        Simple method for subtracting your device to the list for polling
  383. //
  384. // End ---------------------------------------------------------------------
  385. {
  386.     deviceList.detach( theDevice );
  387.     ( ( Device& )theDevice ).theEventManager = 0;
  388. }
  389.  
  390. // End EventManger::subtractDevice //
  391.